package com.yonyou.cyxdms.basedata.service.controller.vehicleproduct;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaoleilu.hutool.lang.StrSpliter;
import com.xiaoleilu.hutool.util.StrUtil;
import com.yonyou.cms.excel.client.entity.DownloadExcelVO;
import com.yonyou.cyxdms.basedata.client.dto.option.VsOptionDTO;
import com.yonyou.cyxdms.basedata.service.dto.ProductTreeDTO;
import com.yonyou.cyxdms.basedata.service.dto.VsProductPriceDTO;
import com.yonyou.cyxdms.basedata.service.dto.vehiclemodel.ColorQueryDTO;
import com.yonyou.cyxdms.basedata.service.dto.sap.CMS008ReqDTO;
import com.yonyou.cyxdms.basedata.service.dto.sap.CMS009ReqDTO;
import com.yonyou.cyxdms.basedata.service.dto.sap.CMSVHCL003ReqDTO;
import com.yonyou.cyxdms.basedata.service.dto.vehiclemodel.ProductQueryDTO;
import com.yonyou.cyxdms.basedata.service.service.product.VsProductService;
import com.yonyou.cyxdms.basedata.service.util.CheckUtil;
import com.yonyou.cyxdms.basedata.service.util.EasyPoiUtil;
import com.yonyou.cyxdms.basedata.service.vo.color.PageColorVO;
import com.yonyou.cyxdms.basedata.service.vo.ExportMsgVO;
import com.yonyou.cyxdms.basedata.service.vo.VsProductDlrVO;
import com.yonyou.cyxdms.basedata.service.vo.productinfo.*;
import com.yonyou.cyxdms.basedata.service.vo.productprice.SaveProductPriceVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import com.yonyou.cms.excel.client.util.ExcelUtils;

/**
 * 车辆主数据
 *
 * @author renwd
 * @since 2019/7/17
 */

@Api(value = "ProductInfoController", tags = {"车辆主数据"})
@RestController
@RequestMapping("/vehicleProduct/productInfo")
public class ProductInfoController {

    @Autowired
    private VsProductService vsProductService;
    @Autowired
    private CheckUtil checkUtil;
    @Autowired
    public ExcelUtils excelUtils;

    /**
     * 车辆主数据
     *
     * @param pageNum
     * @param pageSize
     * @param queryParam
     * @return com.baomidou.mybatisplus.core.metadata.IPage<com.yonyou.cyxdms.basedata.service.vo.productinfo.VsProductVO>
     * @author renwd
     * @since 2019/7/17
     */
    @ApiOperation(value = "车辆主数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", required = true, dataType = "string", paramType = "header")
    })
    @GetMapping
    public IPage<VsProductVO> queryProductInfo(@RequestParam("pageNum") Long pageNum, @RequestParam("limit") Long pageSize, @RequestParam Map<String, Object> queryParam) {
        Page<VsProductVO> page = new Page<>(pageNum, pageSize);
        return vsProductService.queryProductInfo(page, queryParam);
    }
    
    /**
     * 车辆主数据
     *
     * @param pageNum
     * @param pageSize
     * @param queryParam
     * @return com.baomidou.mybatisplus.core.metadata.IPage<com.yonyou.cyxdms.basedata.service.vo.productinfo.VsProductVO>
     * @author zys
     * @since 2020/2/12
     */
    @ApiOperation(value = "车辆主数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", required = true, dataType = "string", paramType = "header")
    })
    @GetMapping("/V2")
    public IPage<VsProductVO> queryProductInfoV2(@RequestParam("pageNum") Long pageNum, @RequestParam("limit") Long pageSize, @RequestParam Map<String, Object> queryParam) {
        Page<VsProductVO> page = new Page<>(pageNum, pageSize);
        return vsProductService.queryProductInfoV2(page, queryParam);
    }

    /**
     * 更新-车辆主数据-整车物料号价格维护表
     *  分类代码、储备高限、储备低限、整车加价率、销售指导价、销售底价
     *
     * @param vsProductPriceDTO
     * @return com.baomidou.mybatisplus.core.metadata.IPage<com.yonyou.cyxdms.basedata.service.vo.productinfo.VsProductVO>
     * @author chenzb
     * @since 2019/11/27
     */
    @ApiOperation(value = "更新-车辆主数据-整车物料号价格维护表 ")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", required = true, dataType = "string", paramType = "header")
    })
    @PutMapping
    public Integer updateProductInfoById(@RequestBody VsProductPriceDTO vsProductPriceDTO) {
        return vsProductService.updateProductInfo(vsProductPriceDTO);
    }

    /**
     * 导出车辆主数据
     *
     * @param queryParam
     * @param request
     * @param response
     * @return void
     * @author renwd
     * @since 2019/7/18
     */
    @ApiOperation(value = "导出车辆主数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", required = true, dataType = "string", paramType = "header")
    })
    @GetMapping("/export/excel")
    public void exportProductInfos(@RequestParam Map<String, Object> queryParam, HttpServletRequest request,
                                   HttpServletResponse response) {
        vsProductService.exportProductInfos(queryParam, request, response);
    }
    
    /**
     * 导出车辆主数据
     *
     * @param queryParam
     * @param request
     * @param response
     * @return void
     * @author zys
     * @since 2020/2/11
     */
    @ApiOperation(value = "导出车辆主数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", required = true, dataType = "string", paramType = "header")
    })
    @GetMapping("/export/excelV2")
    public void exportProductInfosV2(@RequestParam Map<String, Object> queryParam, HttpServletRequest request,
                                   HttpServletResponse response) {
        vsProductService.exportProductInfosV2(queryParam, request, response);
    }

    /**
     * 获取配置树
     *
     * @author ysj
     * @date 2019/09/10
     * @return
     */
    @ApiOperation(value = "获取配置树:0:VALUE返回ID,1:VALUE返回CODE")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", required = true, dataType = "string", paramType = "header")
    })
    @GetMapping("/productTree")
    public List<BrandTreeVO> getProductTree(ProductTreeDTO productTreeDTO) {
        return vsProductService.getProductTree(productTreeDTO);
    }
    
    /**
     * 获取配置树 六级
     *
     * @author zy
     * @date 2020/02/10
     * @return
     */
    @ApiOperation(value = "获取配置树:0:VALUE返回ID,1:VALUE返回CODE")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", required = true, dataType = "string", paramType = "header")
    })
    @GetMapping("/productTreeNew")
    public List<BrandTreeNewVO> getProductTreeNew(ProductTreeDTO productTreeDTO) {
        return vsProductService.getProductTreeNew(productTreeDTO);
    }
    
    
    
    /**
     * 获取配置树(2020/1/6 更改报价管理新增-购买车型-颜色更改为产品)
     *
     * @author zys
     * @date 2020/01/06
     * @return
     */
    @ApiOperation(value = "获取配置树:0:VALUE返回ID,1:VALUE返回CODE")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", required = true, dataType = "string", paramType = "header")
    })
    @GetMapping("/productTree1")
    public List<BrandTreeNewVO> getProductTree1(ProductTreeDTO productTreeDTO) {
        return vsProductService.getProductTree1(productTreeDTO);
    }

    @ApiOperation(value = "添加或更新-车辆主数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", required = true, dataType = "string", paramType = "header")
    })
    @PostMapping("/saveInfo")
    public Integer saveProductInfo(@RequestBody SaveProductPriceVO saveProductPriceVO) {
        return vsProductService.saveProductInfoV2(saveProductPriceVO);
    }

    /**
     * 查询整车编码信息
     *
     * @param pageNum, pageSize, productQueryDTO
     * @return com.baomidou.mybatisplus.core.metadata.IPage<com.yonyou.cyxdms.basedata.service.vo.productinfo.VsProductVO>
     * @author caozx
     * @since 2020/12/14
     */
    @ApiOperation(value = "车辆主数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", required = true, dataType = "string", paramType = "header")
    })
    @GetMapping("/getInfo")
    public IPage<VsProductVO> getProductInfo(@RequestParam("pageNum") Long pageNum, @RequestParam("limit") Long pageSize, ProductQueryDTO productQueryDTO) {
        Page<VsProductVO> page = new Page<>(pageNum, pageSize);
        return vsProductService.getProductInfo(page, productQueryDTO);
    }

    @ApiOperation("excel导出,厂端")
    @PostMapping("/excel/oem")
    public void excelOem(HttpServletResponse response, @RequestBody ProductQueryDTO dto) throws IOException {
        vsProductService.excelOem(dto, response);
    }



    @GetMapping("/getProductPrice")
    public IPage<VsProductVO> getProductPrice(@RequestParam("pageNum") Long pageNum, @RequestParam("limit") Long pageSize, ProductQueryDTO productQueryDTO) {
        Page<VsProductVO> page = new Page<>(pageNum, pageSize);
        if (StrUtil.isNotEmpty(productQueryDTO.getDealerCode())) {
            productQueryDTO.setDealerCode(StrUtil.join("|", StrSpliter.splitByRegex(productQueryDTO.getDealerCode(), ",", 0, true, true)));
        }
        return vsProductService.getProductPrice(page, productQueryDTO);
    }

    /**
     * 查询整车编码信息[无分页]
     *
     * @param  productQueryDTO
     * @return com.baomidou.mybatisplus.core.metadata.IPage<com.yonyou.cyxdms.basedata.service.vo.productinfo.VsProductVO>
     * @author caozx
     * @since 2020/12/14
     */
    @PostMapping("/getInfoList")
    @ApiOperation(value = "车辆主数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", required = true, dataType = "string", paramType = "header")
    })
    public List<VsProductVO> getProductInfoList(@RequestBody VsOptionDTO productQueryDTO) {
        return vsProductService.getProductInfoList(productQueryDTO);
    }

    /**
     * 下发产品信息
     *
     * @param vsProductVO
     * @return java.lang.Integer
     * @author caozx
     * @since 2020/12/17
     */
    @ApiOperation(value = "下发产品信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", required = true, dataType = "string", paramType = "header")
    })
    @PostMapping("/issueInfo")
    public Integer saveProductInfo(@RequestBody ProductInfosVO vsProductVO) {
        return vsProductService.issueProduct(vsProductVO);
    }

    /**
     * 产品信息是否可订购
     *
     * @param vsProductVO
     * @return java.lang.Integer
     * @author caozx
     * @since 2020/12/17
     */
    @ApiOperation(value = "产品信息是否可订购")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", required = true, dataType = "string", paramType = "header")
    })
    @PostMapping("/updateOrderStatus")
    public Integer updateOrderStatus(@RequestBody ProductInfosVO vsProductVO) {
        return vsProductService.updateOrderStatus(vsProductVO);
    }

    @ApiOperation(notes = "整车物料导入模板", value = "整车物料导入模板")
    @GetMapping(value = "/exportProductExportProductsTemplate/excel")
    public void exportProductsTemplate(HttpServletResponse response) throws IOException {
        vsProductService.exportTemplate(response);
    }
    /**
     * 整车编码信息导出
     *
     * @return void
     * @author caozx
     * @since 2020/12/17
     */
    @ApiOperation(notes = "整车物料导出", value = "整车编码信息导出")
    @GetMapping(value = "/exportProduct/excel")
    public void exportProducts(HttpServletResponse response, ProductQueryDTO dto) {
      vsProductService.exportProducts(response,dto);
    }

    @ApiOperation(notes = "整车物料价格导出", value = "整车编码信息导出")
    @GetMapping(value = "/exportProductPrice/excel")
    public void exportProductsPrice(HttpServletResponse response, ProductQueryDTO dto) {
        vsProductService.exportProductsPrice(response,dto);
    }
    @ApiOperation("导出-整车编码信息导出-配置接口")
    @RequestMapping("/exportProduct/excel/config")
    public DownloadExcelVO searchRetailOrderForDealerExcelConfig() throws Exception {
        return excelUtils.getConfigVo(ProductInfosVO.class, "整车物料信息", "整车物料信息");
    }
    /**
     * 查询整车编码信息-已下发
     *
     * @param pageNum, pageSize, productQueryDTO
     * @return com.baomidou.mybatisplus.core.metadata.IPage<com.yonyou.cyxdms.basedata.service.vo.productinfo.VsProductVO>
     * @author caozx
     * @since 2021/01/13
     */
    @ApiOperation(value = "车辆主数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", required = true, dataType = "string", paramType = "header")
    })
    @GetMapping("/getInfoDealer")
    public IPage<VsProductPriceInfoDealerExcelVO> getProductInfoDealer(@RequestParam("pageNum") Long pageNum, @RequestParam("limit") Long pageSize, ProductQueryDTO productQueryDTO) {
        Page<VsProductPriceInfoDealerExcelVO> page = new Page<>(pageNum, pageSize);
        return vsProductService.getProductInfoDealer(page, productQueryDTO);
    }
    @ApiOperation(value = "导出店端物料价格查询")
    @GetMapping("/exportInfoDealer")
    public void getProductInfoDealer(ProductQueryDTO productQueryDTO,HttpServletResponse response) {
        Page<VsProductPriceInfoDealerExcelVO> page = new Page<>(1, -1);
        vsProductService.exportInfoDealer(page, productQueryDTO,response);
    }

    /**
     * 新能源SAP-更新销售指导价
     *
     * @param cms008ReqDTO
     * @return java.lang.Integer
     * @author caozx
     * @since 2021/01/21
     */
    @ApiOperation(value = "新能源SAP-更新销售指导价")
    @PostMapping("/sapUpdateProductPirce")
    public Integer sapUpdateProductPirce(@RequestBody CMS008ReqDTO cms008ReqDTO) {
        return vsProductService.sapUpdateProductPirce(cms008ReqDTO);
    }

    /**
     * 股份SAP-更新销售指导价
     *
     * @param cmsvhcl003ReqDTO
     * @return java.lang.Integer
     * @author caozx
     * @since 2021/02/05
     */
    @ApiOperation(value = "股份SAP-更新销售指导价")
    @PostMapping("/sapSharesUpdateProductPirce")
    public Integer sapSharesUpdateProductPirce(@RequestBody CMSVHCL003ReqDTO cmsvhcl003ReqDTO) {
        return vsProductService.sapSharesUpdateProductPirce(cmsvhcl003ReqDTO);
    }

    /**
     * SAP-批量更新销售指导价(油/电)
     * 
     * @param
     * @return java.lang.Integer
     * @author caozx
     * @since 2021/02/07
     */
    @ApiOperation(value = "SAP-批量更新销售指导价(油/电)")
    @PostMapping("/sapSharesbatchUpdateProductPirce")
    public Boolean sapSharesbatchUpdateProductPirce() {
        return vsProductService.sapSharesbatchUpdateProductPirce();
    }

    /**
     * 根据公告号更新国补金额自动任务
     *
     * @author lyz
     * @date 2021/4/20
     */
    @ApiOperation(value = "自动任务根据公告号更新国补金额")
    @GetMapping("/autoJobSubsidyAmount")
    public void autoJobSubsidyAmount() {
        vsProductService.updateSubsidyAmount();
    }

    /**
     * 新能源SAP-更新国补地补金额
     *
     * @param cms009ReqDTO
     * @return java.lang.Integer
     * @author caozx
     * @since 2021/01/21
     */
    @ApiOperation(value = "新能源SAP-更新国补地补金额")
    @PostMapping("/sapUpdateProductSubsidies")
    public Integer sapUpdateProductSubsidies(@RequestBody CMS009ReqDTO cms009ReqDTO) {
        return vsProductService.sapUpdateProductSubsidies(cms009ReqDTO);
    }

    /**
     * 获取产品信息
     *
     * @param productCode
     * @return com.yonyou.cyxdms.basedata.service.vo.productinfo.VsProductVO
     * @author caozx
     * @since 2021/01/28
     */
    @PostMapping("/getProduct")
    public VsProductVO getProduct(@RequestParam("productCode") String productCode) {
        return vsProductService.getProduct(productCode);
    }

    /**
     * 导入
     *
     * @author lyz
     * @date 2021/3/22
     */
    @PostMapping("/productImportFromExcel")
    @ApiOperation("列表导入")
    public Map<String, List<ExportMsgVO>> productImportFromExcel(@RequestParam("file") MultipartFile file) {
        checkUtil.importFileCheck(file);
        List<SaveProductPriceImportVO> list = EasyPoiUtil.importExcel(file, 1, 1, SaveProductPriceImportVO.class);
        /* 行校验操作 **/
        return vsProductService.checkSaveImportList(list, 1, 1);
    }

    /**
     * 导入
     *
     * @author lx
     * @date 2021/3/22
     */
    @PostMapping("/productImportFromExcel1")
    @ApiOperation("列表导入")
    public Map<String, List<ExportMsgVO>> productImportFromExcel1(@RequestParam("file") MultipartFile file) {
        checkUtil.importCheck(file);
        List<SaveImportExcelVO> list = EasyPoiUtil.importExcel(file, 1, 1, SaveImportExcelVO.class);
        /* 行校验操作 **/
        return vsProductService.checkImportList(list, 1, 1);
    }

    /**
     * 获取产品信息
     *
     * @param productCode
     * @return com.yonyou.cyxdms.basedata.service.vo.VsProductDlrVO
     * @author caozx
     * @since 2021/02/22
     */
    @GetMapping("/getProductNew")
    public VsProductDlrVO getProductNew(@RequestParam("productCode") String productCode) {
        return vsProductService.getProductNew(productCode);
    }
}
